package cn.tom.service;

import ch.qos.logback.classic.net.SyslogAppender;
import cn.tom.dao.BookMapper;
import cn.tom.entity.Book;
import cn.tom.tool.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService{
    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired(required = false)
    BookMapper bookMapper;

    @Autowired
    RedisCache redisCache;

    @Override
    public List<Book> findPageType(Integer num, Integer num2, Integer num3) {
        return this.bookMapper.findPageType(num, num2, num3);
    }

    @Override
    public int getCountType(Integer typeid) {
        return this.bookMapper.getCountType(typeid);
    }

    @Override
    public Book findById(int i) {
        Map<String, Book> books = redisCache.getCacheMap("books");
        if (books.isEmpty()) {
            Map<Integer, Book> bookStock = bookMapper.findAll();
            Map<String,Object> map = new HashMap<>();
            for (Map.Entry<Integer, Book> bookEntry : bookStock.entrySet()) {
                map.put(String.valueOf(bookEntry.getKey()),bookEntry.getValue());
            }
            redisCache.setCacheMap("books",map);
        }
        Book book = books.get(String.valueOf(i));
        return book;
    }

    @Override
    public List<Book> findLike(String str, int i, int i2) {
        return bookMapper.findLike(str,i,i2);
    }

    @Override
    public int findLikeNum(String str) {
        return bookMapper.findLikeNum(str);
    }

    @Override
    public List<Book> findTopSale(int i) {
        return bookMapper.findTopSale(i);
    }

    @Override
    public Book findOne(int id) {
        Map<String, Book> books = redisCache.getCacheMap("books");
        if (books.isEmpty()) {
            Map<Integer, Book> bookStock = bookMapper.findAll();
            Map<String,Object> map = new HashMap<>();
            for (Map.Entry<Integer, Book> bookEntry : bookStock.entrySet()) {
                map.put(String.valueOf(bookEntry.getKey()),bookEntry.getValue());
            }
            redisCache.setCacheMap("books",map);
        }
        Book book = books.get(String.valueOf(id));
        return book;
    }

    @Override
    public int remove(int id) {
        redisCache.deleteObject("books");
        int remove = this.bookMapper.remove(id);
        redisCache.deleteObject("books");
        return remove;
    }

    @Override
    public void update(Book b) {
        redisCache.deleteObject("books");
        this.bookMapper.update(b);
        redisCache.deleteObject("books");
    }

    @Override
    public void add(Book b) {
        redisCache.deleteObject("books");
        this.bookMapper.add(b);
        redisCache.deleteObject("books");
    }

    @Override
    public int findRecord(int uid, int bookid) {
        return this.bookMapper.findRecord(uid,bookid);
    }

    @Override
    public int addRecord(int uid, int bookid) {
        return this.bookMapper.addRecord(uid,bookid);
    }

    @Override
    public List<Book> findAll2() { // 商品与类型
        return bookMapper.findAll2();
    }
}
